package LeetCode_Section;/*
 *
 *@author:    Xavier
 *@data:      2023 - 08 - 18
 *@time:      16:36
 *
 */

//用栈实现队列
//https://leetcode.cn/problems/implement-queue-using-stacks/


import java.util.Stack;

public class UseStackRealizeQueue {
    private Stack<Integer> s1;
    private Stack<Integer> s2;

    public void  MyQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }

    public void push(int x) {
        s1.push(x);
    }

    public int pop() {
        if (!s2.empty()) {
            return s2.pop();
        }else {
            while (!s1.empty()) {
                int val = s1.pop();
                s2.push(val);
            }
            return s2.pop();
        }
    }

    public int peek() {
        if (!s2.empty()) {
            return s2.peek();
        }else {
            while (!s1.empty()) {
                int val = s1.pop();
                s2.push(val);
            }
            return s2.peek();
        }

    }

    public boolean empty() {
        return s1.empty() && s2.empty();
    }
}
